package service

import (
	"encoding/json"
	"fmt"
	"usplatformserver/global"
	"usplatformserver/internal/model"
	"usplatformserver/pkg/zmq"
)

// int为0值时，golang默认处理成null，这里OrganType改为*int类型
type GetDisplayConfigParam struct {
	OrganType *int `form:"organType" binding:"required,gte=0"`
}

// 设置阈值消息请求
type getGetDisplayConfigMsgReq struct {
	MsgType   uint32 `json:"msgType"`
	OrganType uint32 `json:"organType"`
}

// 获取显示配置
func (svc *Service) GetDisplayConfig(addr string, organType int) (*model.DisplayConfig, error) {
	//重组zmq消息
	msgReq := getGetDisplayConfigMsgReq{
		MsgType:   zmq.MsgGetDisplayConfig.MsgType,
		OrganType: uint32(organType),
	}
	msgReqStr, err := json.Marshal(msgReq)
	if err != nil {
		fmt.Println("json.Marshal(msgReq) failed: ", err)
		return nil, err
	}

	rcvMsg, err := zmq.SendMessage(addr, string(msgReqStr))
	if err != nil {
		global.Logger.Infof("get display config failed: %v", err)
	}
	var rcv *model.DisplayConfig
	err = json.Unmarshal([]byte(rcvMsg), &rcv)
	if err != nil {
		fmt.Println("json.Unmarshal(get display config) failed: ", err)
		fmt.Println(rcvMsg)
		return nil, err
	}

	return rcv, nil
}
